package com.ithao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ithao.dao.PermissionDao;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.pojo.Permission;
import com.ithao.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/4/3
 * @description :
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        //删权限角色中间表
        permissionDao.deleteRoleAndPerByPId(id);
        //实际删除操作
        permissionDao.delete(id);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.selectPage(queryPageBean.getQueryParams());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Permission permission) {
        log.info("[权限-添加]data：{}",permission);
        permissionDao.insert(permission);
    }

    @Override
    public void edit(Permission permission) {
       permissionDao.update(permission);
    }

    @Override
    public Permission findById(Integer id) {
        log.info("[权限-根据Id查询]data:{}",id);

        return permissionDao.selectById(id);
    }
}
